package messenger;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.Rectangle2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

public class ChatDialog extends JFrame {
	
	private Toolkit kit = Toolkit.getDefaultToolkit();
	private ContentPanel cp = null;
	private ChatDialog messenger = null;
	private int WIDTH = 300;
	private int HEIGHT = 420;
	private static final int LX = 300;
	private static final int LY = 200;
	
	public ChatDialog(MessengerClient messengerClient, MainWindow f, String to) {
		
		Dimension dim = kit.getScreenSize();
		messenger = this;
		
		setTitle("Talking with " + to);
		setSize(WIDTH, HEIGHT);
		setLocation(LX, LY);
		
		cp = new ContentPanel(messengerClient, f, to);
		MenuPanel mp = new MenuPanel();
		FooterPanel fp = new FooterPanel();
		
		setLayout(null);
		add(mp);
		mp.setBounds(0, 0, WIDTH, 35);
		add(cp);
		cp.setBounds(0, 35, WIDTH, 305);
		add(fp);
		fp.setBounds(0, 340, WIDTH, 50);
		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				cp.getFrame().removeMessenger(cp.getToUser());
				messenger.setVisible(false);
			}
		});
	}
	
	public static void main(String[] args) {
		ChatDialog messenger = new ChatDialog(null, null, "Song");
		messenger.setVisible(true);
	}
	
	public ContentPanel getCP() {
		return cp;
	}
}

class ContentPanel extends JPanel {
	private MessengerClient client = null;
	private String toUser;
	private MainWindow frame = null;
	private JTextArea jt = new JTextArea(3, 20);
	private JButton jb = new JButton("Send");
	private JTextArea ja = new JTextArea(10,20);
	
	public String getToUser() {
		return toUser;
	}

	public MainWindow getFrame() {
		return frame;
	}

	public ContentPanel(MessengerClient messengerClient, MainWindow f, String to) {
		
		client = messengerClient;
		frame = f;
		toUser = to;
		
		ja.setEditable(false);
		JScrollPane jp = new JScrollPane(ja);
		jt.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER) {
					String msg = jt.getText();
					if(msg != null && msg.length() > 0) {
						ja.append(new StringBuilder("You said: ").append(msg).append("\n").toString());
						if(!client.sendMessage(msg, toUser)) {
							ja.append("Failed to send: \"" + msg + "\"\n");
						}
						
					}
				}
			}
		});
		JScrollPane jpt = new JScrollPane(jt);
		jb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String msg = jt.getText();
				if(msg != null && msg.length() > 0) {
					ja.append(new StringBuilder("You said: ").append(msg).append("\n").toString());
					if(!client.sendMessage(msg, toUser)) {
						ja.append("Failed to send: \"" + msg + "\"\n");
					}
					
				}
				jt.setText(null);
			}
		});
		
		setLayout(null);
		
		add(jp);
		jp.setBounds(5, 0, 280, 200);
		
		add(jpt);
		jpt.setBounds(5, 205, 280, 60);
		
		
		add(jb);
		jb.setBounds(215, 270, 70, 25);
	}
	
	public void appendMessage(String msg) {
		ja.append(toUser + " said: " + msg + "\n");
	}
}

class MenuPanel extends JPanel {
	public MenuPanel() {
		JMenuBar menuBar = new JMenuBar();
		
		JMenu file = new JMenu("File");
		JMenu edit = new JMenu("Edit");
		
		JMenuItem close = new JMenuItem("Close");
		close.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK));
		
		file.add(close);
		menuBar.add(file);

		menuBar.add(edit);
		
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		add(menuBar);
	}
	
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
//		g.setColor(Color.RED);
//		g.drawString("Part of Menu", 5, 10);
	}
}

class FooterPanel extends JPanel {
	public FooterPanel() {
//		add(new JButton("Footer"));
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(Color.RED);
		g2.draw(new Rectangle2D.Double(5,0,280,40));
	}
}
